package com.ztjava.core.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.jfinal.kit.LogKit;
import com.jfinal.kit.PathKit;

public class FileUtil {
	public static String readFileToString(String path){
		return readFileToString(path,"UTF-8");
	}
	//读取文件
	public static String readFileToString(String path,String code){
		InputStream inputStream = null;
		BufferedReader reader = null;
		String laststr = "";
		try{
			inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);	
			if(inputStream!=null){
				InputStreamReader inputStreamReader = new InputStreamReader(inputStream, code);
				reader = new BufferedReader(inputStreamReader);
				String tempString = null;
				while((tempString = reader.readLine()) != null){
					laststr += tempString;
				}
				reader.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (inputStream != null) try {inputStream.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);}
		}
		return laststr;
	}
	public static String getAppPath(){
		String path = PathKit.getWebRootPath();
		if(path.endsWith(File.separator)){
			path = path.substring(0, path.length()-1);
		}
		path = path.substring(path.lastIndexOf(File.separator),path.length()).replace(File.separator,"/");
		if(path.toUpperCase().equals("/ROOT")){
			path = "";
		}
		return path;
	}
	
	public static String getFileExt(String filename){
		return filename.substring(filename.lastIndexOf("."),filename.length()).toLowerCase();
	}	
	public static String getWebPath(){
		String path = PathKit.getWebRootPath();
		if(path.endsWith(File.separator)){
			path = path.substring(0, path.length()-1);
		}
		path = path.substring(0,path.lastIndexOf(File.separator));
		return path;
	}
}
